import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import Login from '../views/Login.vue'
import Layout from '../views/Layout.vue'
import NewBooks from '../views/NewBooks.vue'
import BookBorrow from '../views/BookBorrow.vue'
import BookDetail from '../views/BookDetail.vue'
import CurrentBorrow from '../views/CurrentBorrow.vue'
import BorrowRecords from '../views/BorrowRecords.vue'
import Favorites from '../views/Favorites.vue'
import Reservations from '../views/Reservations.vue'
import Notifications from '../views/Notifications.vue'
import Profile from '../views/Profile.vue'
import UserManagement from '../views/UserManagement.vue'
import Reports from '../views/Reports.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/new-books',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'new-books',
        name: 'NewBooks',
        component: NewBooks,
        meta: { title: '新书推荐' }
      },
      {
        path: 'book-borrow',
        name: 'BookBorrow',
        component: BookBorrow,
        meta: { title: '图书借阅' }
      },
      {
        path: 'book-detail/:id',
        name: 'BookDetail',
        component: BookDetail,
        meta: { title: '图书详情' }
      },
      {
        path: 'current-borrow',
        name: 'CurrentBorrow',
        component: CurrentBorrow,
        meta: { title: '当前借阅' }
      },
      {
        path: 'borrow-records',
        name: 'BorrowRecords',
        component: BorrowRecords,
        meta: { title: '借阅记录' }
      },
      {
        path: 'favorites',
        name: 'Favorites',
        component: Favorites,
        meta: { title: '我的收藏' }
      },
      {
        path: 'reservations',
        name: 'Reservations',
        component: Reservations,
        meta: { title: '我的预约' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: Notifications,
        meta: { title: '通知中心' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
        meta: { title: '个人中心' }
      },
      {
        path: 'user-management',
        name: 'UserManagement',
        component: UserManagement,
        meta: { title: '用户管理', requiresAdmin: true }
      },
      {
        path: 'reports',
        name: 'Reports',
        component: Reports,
        meta: { title: '报表与统计', requiresAdmin: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next({ name: 'Login' })
  } else if (to.meta.requiresAdmin && !authStore.isAdmin) {
    next({ path: '/' })
  } else if (to.path === '/login' && authStore.isAuthenticated) {
    next({ path: '/' })
  } else {
    next()
  }
})

export default router

